#include "queue_demo.h"

UINT32 g_testTaskID001;
UINT32 g_testQueueID001;
QUEUE_INFO_S queueInfo;
CHAR   send_buff[QUEUE_SHORT_BUFFER_LENGTH] = "Hi Task!";
CHAR   send_buff1[QUEUE_SHORT_BUFFER_LENGTH] = "补的消息";
CHAR   recv_buff[QUEUE_SHORT_BUFFER_LENGTH] = " ";
CHAR   queue_send_buff[QUEUE_SHORT_BUFFER_LENGTH] = "Hi Demo!";
CHAR   queue_recv_buff[QUEUE_SHORT_BUFFER_LENGTH] = " ";

static VOID QueueTaskF01(VOID)
{
    UINT32 ret;
    printf("\n+++++++++++++QueueTaskF01:start\n");
     
    ret = LOS_QueueReadCopy(g_testQueueID001, &queue_recv_buff, QUEUE_BASE_MSGSIZE, 0);
    if(ret==LOS_OK)
        printf("\n+++++++++++++QueueTaskF01:message received = %s\n",queue_recv_buff);

    ret = LOS_QueueReadCopy(g_testQueueID001, &queue_recv_buff, QUEUE_BASE_MSGSIZE, LOS_WAIT_FOREVER);
    if(ret==LOS_OK)
        printf("\n+++++++++++++现在收到消息了QueueTaskF01:message received = %s\n",queue_recv_buff);

    ret = LOS_QueueWriteCopy(g_testQueueID001, &queue_send_buff, QUEUE_BASE_MSGSIZE, 0);
    if(ret==LOS_OK)
        printf("\n+++++++++++++QueueTaskF01:message send = %s\n",queue_send_buff);

    return;
}

VOID PrintQueueInfo(VOID)
{
    printf("\n-------------Queue Demo:queueID =%d\n",queueInfo.queueID);
    printf("\n-------------Queue Demo:queueLen =%d\n",queueInfo.queueLen);
    printf("\n-------------Queue Demo:queueHead=%d\n",queueInfo.queueHead);
    printf("\n-------------Queue Demo:queueTail =%d\n",queueInfo.queueTail);
    printf("\n-------------Queue Demo:writeableCnt =%d\n",queueInfo.writableCnt);
    printf("\n-------------Queue Demo:readableCnt =%d\n",queueInfo.readableCnt);
    printf("\n-------------Queue Demo:wait read task =%d\n",queueInfo.waitReadTask[OS_WAIT_TASK_ARRAY_LEN]);
    printf("\n-------------Queue Demo:wait write task =%d\n",queueInfo.waitWriteTask[OS_WAIT_TASK_ARRAY_LEN]);
    printf("\n-------------Queue Demo:wait mem task =%d\n",queueInfo.waitMemTask[OS_WAIT_TASK_ARRAY_LEN]);
}


VOID Queue_Demo(VOID)
{
    UINT32 ret;
    //Create a queue here
    ret = LOS_QueueCreate("Q1",2, &g_testQueueID001, 0, QUEUE_BASE_MSGSIZE);
    if(ret==LOS_OK)
    printf("\n~~~~~~~~~~~~~~~~~~This is liweili homework~~~~~~~~~~~~~~~~~~\n");
    printf("\n+++++++++++++Queue Demo:queue create ok! the queue ID = %d\n",g_testQueueID001);

    ret = LOS_QueueInfoGet(g_testQueueID001, &queueInfo);
    if(ret==LOS_OK)
    printf("\n+++++++++++++Queue Demo:queue info get ok!\n");

    //Create the QueueTaskF01
    TSK_INIT_PARAM_S queue_task1;
    (void)memset_s(&queue_task1, sizeof(TSK_INIT_PARAM_S), 0, sizeof(TSK_INIT_PARAM_S));
    queue_task1.pfnTaskEntry = (TSK_ENTRY_FUNC)QueueTaskF01;
    queue_task1.uwStackSize  = TASK_STACK_SIZE_TEST;
    queue_task1.pcName       = "queueTsk1";
    queue_task1.usTaskPrio   = TASK_PRIO_TEST;
    queue_task1.uwResved   = LOS_TASK_STATUS_DETACHED;

    ret = LOS_TaskCreate(&g_testTaskID001, &queue_task1);
    if(ret==LOS_OK)
        printf("\n+++++++++++++Queue Demo:QueueTaskF01 created ok! taskID = %d\n",g_testTaskID001);
    
    ret = LOS_QueueWriteCopy(g_testQueueID001, &send_buff, QUEUE_BASE_MSGSIZE, LOS_WAIT_FOREVER);
    if(ret==LOS_OK)
        printf("\n+++++++++++++Queue Demo:message send = %s with address = 0x%x\n",send_buff,send_buff);

    LOS_TaskDelay(20); 

    //现在才写消息
    ret = LOS_QueueInfoGet(g_testQueueID001, &queueInfo);
    if(ret==LOS_OK)
        printf("\n+++++++++++++Queue Demo:queue info get ok!\n");
    printf("\n此时的队列可读节点数为：%d\n",queueInfo.readableCnt);

    ret = LOS_QueueWriteCopy(g_testQueueID001, &send_buff1, QUEUE_BASE_MSGSIZE, LOS_WAIT_FOREVER);
    if(ret==LOS_OK)
        printf("\n+++++++++++++Queue Demo:我现在再写消息让你读message send = %s with address = 0x%x\n",send_buff1,send_buff1);

    LOS_TaskDelay(20);

    ret = LOS_QueueReadCopy(g_testQueueID001, &recv_buff, QUEUE_BASE_MSGSIZE, LOS_WAIT_FOREVER);
    printf("\n++++++++++++++Queue Demo:message received = %s \n",recv_buff);
    ret = LOS_QueueReadCopy(g_testQueueID001, &recv_buff, QUEUE_BASE_MSGSIZE, LOS_WAIT_FOREVER);
    printf("\n++++++++++++++Queue Demo:message received = %s \n",recv_buff);
    

    ret = LOS_QueueDelete(g_testQueueID001);

    ret = LOS_TaskDelete(g_testTaskID001);
   
    return ret;
}
